//
//  StringUtil.h
//  DiceWar
//
//  Created by xiaofeng on 20/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#ifndef CLASS_GAME_UTIL_STRINGUTIL_H_
#define CLASS_GAME_UTIL_STRINGUTIL_H_

#include <string>
#include <sstream>

class StringUtil
{
public:
    template<typename T> 
    static std::string ToString(T value);
    
    template<typename T> 
    static T ToValue(const std::string& strValue);
    
    static void ToLower(std::string& str);
    static void ToUpper(std::string& str);
    
    static void TrimBothSides(std::string& str);
    static void TrimLeft(std::string& str);
    static void TrimRight(std::string& str);
    static bool IsTrimChar(char ch);
    
    static std::wstring StringToWString(const std::string& str);
    static std::wstring StringToWString(const char* str);
    static std::string WStringToString(const std::wstring& wstr);
    
private:
    static const int s_kFloatPrecision;
};

template<typename T> 
std::string StringUtil::ToString(T value)
{
    std::ostringstream ss;
    ss << value;
    return ss.str();
}

template<>
inline std::string StringUtil::ToString<float>(float value)
{
    std::ostringstream ss;
    ss.precision(s_kFloatPrecision);
    ss << std::fixed << value;
    return ss.str(); 
}

template<typename T> 
T StringUtil::ToValue(const std::string& strValue)
{
    T result;
    std::istringstream iss(strValue);
    iss >> result;
    return result;
}

#endif //CLASS_GAME_UTIL_STRINGUTIL_H_
